package infos

import (
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type enterInfo struct {
	ID      string        `json:"id"`
	Images  []string      `json:"images"`  // - 组织图片
	Columns []enterColumn `json:"columns"` // 组织展板显示栏目
}
type enterColumn struct {
	Name     string `json:"name"`      // 栏目名称
	ColumnID string `json:"column_id"` // 栏目编号
}

// 获取栏目信息
func (c infoCtrl) getEnterprises(ctx *gin.Context) {

	enterid := ctx.Param("enterid")
	if enterid == "" {
		c.ErrBadRequest(ctx, 1, "没有设置编号")
		return
	}
	if !strings.HasPrefix(enterid, "002-") {
		enterid = "002-" + enterid
	}
	c.getBoard(ctx, enterid)

}

// 获取栏目信息
func (c infoCtrl) getEnterprisesCarouse(ctx *gin.Context) {

	enterid := ctx.Param("enterid")
	if enterid == "" {
		c.ErrBadRequest(ctx, 1, "没有设置编号")
		return
	}
	if !strings.HasPrefix(enterid, "002-") {
		enterid = "002-" + enterid
	}

	c.getBoardCarouse(ctx, enterid, "/company_show_new")

}

// 获取展板信息
func (c infoCtrl) getBoardCarouse(ctx *gin.Context, boradid, detailurl string) {
	dctx := c.DDDContext(ctx)
	org, err := c.oar.GetOrg(dctx, boradid)
	if err != nil {
		c.ErrServerError(ctx, 2, "获取指定失败", errors.Wrapf(err, "查询展板信息(%v)失败.", boradid))
		return
	}
	if org == nil {
		c.ErrBadRequest(ctx, 10, "没有开通展板")
		return
	}
	if org.Status == -1 {
		c.ErrBadRequest(ctx, 11, "没有开通展板")
		return
	}
	var resp []respCarouse
	url := detailurl + boradid
	for _, v := range org.Icon {
		resp = append(resp, respCarouse{
			URL:   url,
			Image: v,
		})
	}
	c.Success(ctx, resp)

}

// 获取展板信息
func (c infoCtrl) getBoard(ctx *gin.Context, boardid string) {

	dctx := c.DDDContext(ctx)
	enter, err := c.oar.GetOrg(dctx, boardid)
	if err != nil {
		c.ErrServerError(ctx, 2, "获取指定失败", errors.Wrapf(err, "查询展板信息(%v)失败.", boardid))
		return
	}
	if enter == nil {
		c.ErrBadRequest(ctx, 10, "没有开通展板")
		return
	}
	if enter.Status == -1 {
		c.ErrBadRequest(ctx, 11, "没有开通展板")
		return
	}
	pi := enterInfo{
		ID:     boardid,
		Images: enter.Icon,
	}
	var cols []enterColumn

	for _, v := range enter.Columnids {
		col, err := c.car.GetColumn(dctx, v)
		if err != nil {
			c.ErrBadRequest(ctx, 13, "查询栏目失败", err)
			return
		}
		pc := enterColumn{
			Name:     col.Name,
			ColumnID: v,
		}
		cols = append(cols, pc)

	}
	pi.Columns = cols
	c.Success(ctx, pi)
}
